/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import util.HibernateUtil;
import java.util.List;
import pojo.Dienthoai;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author Jackhit
 */
public class DienThoaiDAO {
    public static List<Dienthoai> layDanhSachDienThoai() {
        List<Dienthoai> dsDienThoai = null;
         Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Dienthoai";
            Query query = session.createQuery(hql);
            dsDienThoai = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsDienThoai;
    }

    public static boolean themDienThoai(Dienthoai dienThoai) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(dienThoai);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public  static boolean capNhatDienThoai(Dienthoai dienThoai) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(dienThoai);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static boolean xoaDienThoai(Dienthoai dienThoai) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(dienThoai);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }
}
